1. Field of Invention
The present invention relates to a method for memory management, and more particularly to a method for memory management, wherein pages are recorded through shifting bits in the register.
2. Description of Related Art
Generally, when a computer is executing a program, the program code is first loaded in a memory, and then read and calculated by a computer microprocessor, after that, the result is obtained. Although the memory provides a relatively high accessing speed, it is rather expensive, such that the memory capacity for a common computer is limited. However, the memory required by the computer when executing programs always exceeds the physical memory capacity, thus, the concept of Virtual Memory is derived, which refers to dividing a block from the hard disk to be used as the memory so as to solve the problem of insufficient memory space.
Currently, the virtual memory management architecture adopts the method of page management. The address spaces of the virtual memory and the physical memory are divided into a plurality of pages, and then, through the page management, a part of the data required during the current execution of the application is stored in the pages of the physical memory, and other parts of the data are stored in the pages of the virtual memory. According to the requirement for executing programs by the microprocessor, the data stored in the pages of the virtual memory is flexibly loaded into the physical memory for being used by the microprocessor. With the above method, the system is able to execute programs within a limited memory space.
The conventional page management rule includes two methods, namely Least Recently Use (LRU) and First In First Out (FIFO). The LRU means that when performing the page replacement, the least recently used page in the memory block is replaced as a new page; and the FIFO means that the page that is accessed first is the one being taken out first and then replaced as a new page. Moreover, the so-called page management refers to dividing the register region into a plurality of pages, wherein each page corresponds to a memory block and each page is provided with a counter for counting the existing time of each page (i.e., the accumulated time after the data in the memory block corresponding to the page is accessed) for being used as the reference for page replacement.
Herein, the LRU page management rule is taken as an example. FIG. 1 is a flow chart of a conventional memory page management. Referring to FIG. 1, after a request for reading data is received form the microprocessor (Step S110), whether the data is stored in the memory block corresponding to each page is determined (Step S120). If the data is not stored in the memory block corresponding to the page, the page having the largest counting value is found out in the register (Step S130), and then, the required data is accessed from an external storage device and stored in the memory block corresponding to the page (Step S150). If the data has already been stored in the memory block corresponding to the page, the value of the counter of the page for storing the data is set to be 0, and the values of the counters of other pages are increased by 1 (Step S140). Finally, the data read from the memory block corresponding to the above page is output (Step S160).
In the above flow of the memory page management, if the data is not stored in the memory block corresponding to the page, the microprocessor must read all the pages in the physical memory and compare the counting values of the counters for the pages, so as to find out the page to be replaced. As for the above example, the register is divided into 8 pages, each page must be provided with an 8-bit counter, and during the paging process, 8 memory reading, 8 memory writing and 15 logic calculation (including 8 data comparisons and 7 data additions) are required. It can be known that the conventional method for additionally disposing a counter for recording the existing time of each page not only wastes the memory space, but also consumes lots of calculation resources of the microprocessor because the microprocessor has to access the value of the counter and perform logic calculations for many times.